﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using ComponentFactory.Krypton.Toolkit;
using SceneEngine;

namespace SceneEditor.UI
{
    public partial class AddLightForm : KryptonForm
    {
        public Vector3 direction = new Vector3(-1, 1, -1);
        public float intensity = 1.0f;
        public float range = 500f;

        public int type;

        public AddLightForm()
        {
            InitializeComponent();
            cmbType.SelectedIndex = 0;
            pDirectional.Location = new System.Drawing.Point(16, 63);
            pPoint.Location = new System.Drawing.Point(16, 63);
        }

        private void tbDirection_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != ' ' && e.KeyChar != '-' && e.KeyChar != '.')
                e.Handled = true;
        }

        private void tbIntensity_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        private void tbDirection_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string[] strValues = tbDirection.Text.Split();

                if (strValues.Length != 3)
                {
                    MessageBox.Show("Entered Property is not Valid");
                    return;
                }

                float[] integerValues = new float[strValues.Length];
                for (int i = 0; i < strValues.Length; i++)
                {
                    float newValue = 0;
                    if (float.TryParse(strValues[i], out newValue))
                        integerValues[i] = newValue;
                }

                direction = new Vector3(integerValues[0], integerValues[1], integerValues[2]) * -1;
            }
        }

        private void tbDirection_Leave(object sender, EventArgs e)
        {
            string[] strValues = tbDirection.Text.Split();

            if (strValues.Length != 3)
            {
                MessageBox.Show("Entered Property is not Valid");
                return;
            }

            int[] integerValues = new int[strValues.Length];
            for (int i = 0; i < strValues.Length; i++)
            {
                int newValue = 0;
                if (int.TryParse(strValues[i], out newValue))
                    integerValues[i] = newValue;
            }

            direction = new Vector3(integerValues[0], integerValues[1], integerValues[2]) * -1;
        }

        private void tbIntensity_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                float newValue = 0;
                if (float.TryParse(tbIntensity.Text, out newValue))
                    intensity = newValue;
            }
        }

        private void tbIntensity_Leave(object sender, EventArgs e)
        {
            float newValue = 0;
            if (float.TryParse(tbIntensity.Text, out newValue))
                intensity = newValue;
        }

        private void tbRange_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                float newValue = 0;
                if (float.TryParse(tbRange.Text, out newValue))
                    range = newValue;
            }
        }

        private void tbRange_Leave(object sender, EventArgs e)
        {
            float newValue = 0;
            if (float.TryParse(tbRange.Text, out newValue))
                range = newValue;
        }

        private void AddLightForm_Load(object sender, EventArgs e)
        {

        }

        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbType.SelectedIndex == 0)
            {
                pDirectional.Enabled = true;
                pDirectional.Visible = true;

                pPoint.Enabled = false;
                pPoint.Visible = false;
            }

            else if (cmbType.SelectedIndex == 1)
            {
                pPoint.Enabled = true;
                pPoint.Visible = true;

                pDirectional.Enabled = false;
                pDirectional.Visible = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (tbName.Text == string.Empty)
            {
                MessageBox.Show("Please insert a name for the light");
                return;
            }

            else if (cmbType.SelectedIndex == 0)
            {
                if (tbDirection.Text == string.Empty)
                {
                    MessageBox.Show("Please insert a direction for the light");
                    return;
                }

                if (tbIntensity.Text == string.Empty)
                {
                    MessageBox.Show("Please insert an intensity for the light");
                    return;
                }
            }

            else if (cmbType.SelectedIndex == 1)
            {
                if (tbRange.Text == string.Empty)
                {
                    MessageBox.Show("Please insert a range for the light");
                    return;
                }

                if (tbIntensityPoint.Text == string.Empty)
                {
                    MessageBox.Show("Please insert an intensity for the light");
                    return;
                }
            }
            this.type = cmbType.SelectedIndex;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void AddLightForm_Shown(object sender, EventArgs e)
        {
            if (EngineGlobals.Scene.Contains(tbName.Text))
            {
                tbName.Text = string.Empty;
            }
        }
    }
}
